﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Xml.Linq;
using System.IO;
using System.Reflection;

namespace CodeCounsel.SharePoint.DevTools
{
    [ComVisible(true)]
    [Guid(VSPackageGuids.SharePointReferencesPageText)]
    [ClassInterface(ClassInterfaceType.AutoDual)]
    partial class SharePointReferenceView
        : ComponentPickerPage
    {
        const string Namespace = "urn:CodeCounsel.SharePoint.DevTools.Schema.References";

        public string ReferencePath { get; set; }

        public SharePointReferenceView()
        {
            InitializeComponent();
            DoubleBuffered = true;
        }

        protected override void InitializeItems()
        {
            _referencesList.Items.Clear();
            _referencesList.Groups.Clear();
            string sharePointInstallFolder = ProjectUtilities.GetSharePointInstallRoot();
            XDocument document = XDocument.Load(ReferencePath);
            XNamespace ns = Namespace;
            foreach (XElement groupElement in document.Root.Elements(ns + "referenceGroup"))
            {
                string groupName = groupElement.Attribute("name").Value;
                ListViewGroup group = _referencesList.Groups.Add(groupName, groupName);
                foreach (XElement referenceElement in groupElement.Elements(ns + "reference"))
                {
                    string relativePath = referenceElement.Attribute("path").Value;
                    string fullPath = Path.Combine(sharePointInstallFolder, relativePath);
                    AssemblyName assemblyName = AssemblyName.GetAssemblyName(fullPath);
                    ListViewItem item = new ListViewItem(assemblyName.Name);
                    item.SubItems.Add(assemblyName.Version.ToString());
                    item.SubItems.Add(fullPath);
                    item.Group = group;
                    _referencesList.Items.Add(item);
                }
            }
        }

        protected override ComponentPickerItem[] GetSelection()
        {
            List<ComponentPickerItem> items = new List<ComponentPickerItem>();
            foreach (ListViewItem item in _referencesList.SelectedItems)
            {
                items.Add(new ComponentPickerItem()
                {
                    Title = item.Text,
                    Path = item.SubItems[2].Text
                });
            }
            return items.ToArray();
        }

        protected override void ClearSelection()
        {
            List<ListViewItem> items =  _referencesList.SelectedItems.OfType<ListViewItem>().ToList();
            foreach (ListViewItem item in items)
            {
                item.Selected = false;
            }
        }

        protected override void SetSelectionMode(bool multiSelect)
        {
            _referencesList.MultiSelect = multiSelect;
        }
    }
}